package yxy.game.pm2.module.niubee;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.task;
import yxy.cherry.data.bean.weapondata;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.pubsub.TaskPublisher;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class NiuBeeModule extends PlayerModule {
	// bean===========================================================================

	private int progress;// 激活神器进度
	private int level;// 等级
	private int exp;// 经验
	private int refineLevel;// 精炼等级
	private int soulLevel;// 铸魂等级
	private int skillLevel;// 技能等级

//	private ArrayList<String> taskCodes = new ArrayList<>();// 任务
//	private ArrayList<String> allTaskCodes = new ArrayList<>();// 任务

	private HashMap<Integer, ArrayList<String>> tasks = new HashMap<>();

	public int getProgress() {
		return progress;
	}

	public void setProgress(int progress) {
		this.progress = progress;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getExp() {
		return exp;
	}

	public void setExp(int exp) {
		this.exp = exp;
	}

	public int getRefineLevel() {
		return refineLevel;
	}

	public void setRefineLevel(int refineLevel) {
		this.refineLevel = refineLevel;
	}

	public int getSoulLevel() {
		return soulLevel;
	}

	public void setSoulLevel(int soulLevel) {
		this.soulLevel = soulLevel;
	}

	public int getSkillLevel() {
		return skillLevel;
	}

	public void setSkillLevel(int skillLevel) {
		this.skillLevel = skillLevel;
	}

	public HashMap<Integer, ArrayList<String>> getTasks() {
		return tasks;
	}

	public void setTasks(HashMap<Integer, ArrayList<String>> tasks) {
		this.tasks = tasks;
	}

	// func===========================================================================
	public void act() {
		Collection<Task> tasks = task();
		if (tasks.size() <= 0) {
			return;
		}
		for (Task task : tasks) {
			if (!task.isFinish() || !task.isReward()) {
				return;
			}
		}
		int progress = getProgress() + 1;
		setProgress(progress);
		set();

		TaskPublisher.getInstance().publish(getPlayerCode());
		TaskHandle.handle(getPlayerCode(), TaskType.统计型_神器_解锁_个数, 1);
	}

	public Collection<Task> task() {
		ArrayList<Task> tasks = new ArrayList<>();
		ArrayList<String> list = getTasks().get(getProgress() + 1);
		if (list == null) {
			return tasks;
		}

		for (String code : list) {
			Task task = Task.get(getPlayerCode(), code);
			tasks.add(task);
		}
		return tasks;
	}

	private void initTask() {
		Map<String, weapondata> all = DataCenter.getAllData(weapondata.class);
		for (weapondata data : all.values()) {
			int[] taskIds = data.getTaskid();
			ArrayList<String> list = new ArrayList<>();
			for (int i = 0; i < taskIds.length; i++) {
				int taskId = taskIds[i];
				task taskdata = DataCenter.getData(taskId, task.class);
				Task task = Task.create(getPlayerCode(), taskdata, data.getId());
				list.add(task.getCode());
			}
			getTasks().put(data.getId(), list);
		}
	}

	// redis==========================================================================
	private static String key(String playerCode) {
		return String.format("PLAYER:%s:MODULE:NIUBEE", playerCode);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public NiuBeeModule get(String playerCode) {
		try {
			NiuBeeModule module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), NiuBeeModule.class);
				if(module != null){module.set();}//把数据同步到redis
			}

			if (module == null) {
				module = create(playerCode);
			}
			if (module.getTasks().size() <= 0) {
				module.initTask();
				module.set();
			}
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	static public NiuBeeModule create(String playerCode) {
		NiuBeeModule module = new NiuBeeModule();
		module.setPlayerCode(playerCode);
		module.setSkillLevel(1);
		module.initTask();
		module.set();
		return module;
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				NiuBeeModule niuBeeModule = getv(key(playerCode));
				if(niuBeeModule != null)MySQLCacheDAO.setV(key, niuBeeModule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
